#!/bin/bash
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   runtest.sh of /tools/systemtap/Sanity/stap-server-basic-sanity
#   Description: stap-server-basic-sanity
#   Author: Martin Cermak <mcermak@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2013 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE. See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/share/beakerlib/beakerlib.sh || exit 1

CMD='stap'
BIN=$(which --skip-alias $CMD)
PACKAGE="${PACKAGE:-$(rpm -qf --qf='%{name}\n' $BIN | head -1)}"

export DEBUGINFOD_URLS="https://debuginfod.fedoraproject.org/ $DEBUGINFOD_URLS"
export DEBUGINFOD_TIMEOUT=300

function service_stap_server() {
    action=$1
    retval=${2:-0}
    sleep 5
    rlLogInfo "PACKAGE=$PACKAGE"
    if echo $PACKAGE | grep -q devtoolset; then
	__i=$(echo $PACKAGE | awk -F- '{print $2}')
	rlRun "service devtoolset-$__i-stap-server $action" $retval
    elif echo $PACKAGE | grep -q gcc-toolset; then
	__i=$(echo $PACKAGE | awk -F- '{print $3}')
	rlRun "service gcc-toolset-$__i-stap-server $action" $retval
    else
	rlRun "service stap-server $action" $retval
    fi
    sleep 5
}

_rhelmajor=$(rpm --eval '%{rhel}')

rlJournalStart
    rlPhaseStartSetup
        rlRun "TMPDIR=$(mktemp -d)"
        rlRun "pushd $TMPDIR"
        rlAssertRpm dbus
        rlAssertRpm avahi
        rlAssertRpm net-tools # because of netstat
    rlPhaseEnd

    rlPhaseStart FAIL "sanity check"
        rlRun "stap -v -e 'probe begin { log(\"hello\"); exit() }'"
    rlPhaseEnd

    rlPhaseStartTest
        rlRun "service iptables stop" 0-255
        rlRun "service firewalld stop" 0-255
        service_stap_server stop
        test $_rhelmajor -le 8 && \
            rlRun "service messagebus restart"
        rlRun "service avahi-daemon restart"
        service_stap_server start

        rlRun "netstat -tlp | grep stap"
        rlRun "SERVER_PORT=$( netstat -tlpn | awk '/stap/ {print $4}' | grep -o '[0-9]*$' )"

        for SERVER_IP in '127.0.0.1' '[::1]'; do
            rlLogInfo "Testing SERVER_IP=$SERVER_IP"
            rlRun "stap --trust-servers=ssl,signer,all-users,no-prompt --use-server=$SERVER_IP:$SERVER_PORT"
            rlRun "stap --use-server=$SERVER_IP:$SERVER_PORT -v -e 'probe begin { log(\"hello\"); exit() }' 2>&1 | tee output.log"
            rlRun "grep 'Using a compile server' output.log"
            rlRun "grep '^hello$' output.log"
        done
    rlPhaseEnd

    rlPhaseStartCleanup
        service_stap_server stop
        rlRun "popd"
        rlRun "rm -r $TMPDIR"
    rlPhaseEnd
rlJournalPrintText
rlJournalEnd
